import sys
import image_resource                                 #导入图片资源
from math import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QApplication, QMainWindow
from mini_calc_ui import Ui_MainWin                   #导入主界面
from help_dialog import Ui_helpDialog                 #导入帮助界面
from donate_dialog import Ui_donateDialog             #导入捐助界面


class MyMainWindow(QMainWindow, Ui_MainWin):          #定义主界面类
    def __init__(self):
        super(MyMainWindow, self).__init__()
        self.setupUi(self)                            # 建立UI界面
        #self.setWindowFlags(Qt.WindowStaysOnTopHint) #非常可惜,保持窗口顶层和无标题栏不能共存
        self.setWindowFlags(Qt.FramelessWindowHint)  #设置窗口为无标题栏风格
        self.setAttribute(Qt.WA_TranslucentBackground)  #设置窗口背景透明
        self.closeWidget_button.clicked.connect(self.close)  #绑定关闭窗口按钮关闭窗口
        self.minimizeWidget_button.clicked.connect(self.showMinimized)  #绑定最小化窗口按钮最小化窗口
        self.help_button.clicked.connect(self.showhelpdialog)   #绑定帮助按钮弹出帮助窗口
        self.equal_button.clicked.connect(self.click_equal)     #绑定等号按钮和回调函数
        self.clear_button.clicked.connect(self.show_input.setFocus)  #点击清除按钮输入框得到焦点
        self.show_input.textChanged.connect(self.click_equal)   #输入框内容变化时调用等号处理函数

    def mousePressEvent(self, event):                           #
        if event.button() == Qt.LeftButton:                     #
            self.m_flag = True                                  #
            self.m_position = event.globalPos() - self.pos()    #
            self.setCursor(Qt.OpenHandCursor)                   #此三函数
                                                                #用于移动
    def mouseMoveEvent(self, QMouseEvent):                      #窗口
        if Qt.LeftButton and self.m_flag:                       #
            self.move(QMouseEvent.globalPos() - self.m_position)#
                                                                #
    def mouseReleaseEvent(self, QMouseEvent):                   #
        self.m_flag = False                                     #
        self.setCursor(Qt.ArrowCursor)                          #

    def click_equal(self):                          #等号按钮的处理函数
        try:
            contain = eval(self.show_input.text())  #获取输入的文本并转换为表达式
            length = str(round(contain, 10))        #将精度设置为小数点后10位
            self.show_result.setText(length)        #将计算结果输出到结果框
        except:
            self.show_result.setText('')            #如果表达式无法计算，则不显示内容

    def paintEvent(self, event):                    #绘制窗口背景
        p = QPainter(self)
        p.setBrush(QBrush(QPixmap(':/image/image/background.jpg')))#加入背景图片
        p.setRenderHint(QPainter.Antialiasing)
        p.drawRoundedRect(0, 0, self.width(), self.height(), 15, 15)#窗口倒圆角

    def showhelpdialog(self):              #定义弹出帮助对话框
        self.helpWin = helpDialog()
        self.helpWin.show()

    def mousePressEvent(self, event):              #以下三函数用于移动窗口
        if event.button() == Qt.LeftButton:
            self.m_flag = True
            self.m_position = event.globalPos() - self.pos()
            self.setCursor(Qt.OpenHandCursor)

    def mouseMoveEvent(self, QMouseEvent):
        if Qt.LeftButton and self.m_flag:
            self.move(QMouseEvent.globalPos() - self.m_position)

    def mouseReleaseEvent(self, QMouseEvent):
        self.m_flag = False
        self.setCursor(Qt.ArrowCursor)




class helpDialog(QMainWindow, Ui_helpDialog):         #帮助对话框类
    def __init__(self):
        super(helpDialog, self).__init__()
        self.setupUi(self)
        self.move(myWin.x()+myWin.width(), myWin.y())  #让对话框靠主窗口右边停靠
        self.setAttribute(Qt.WA_TranslucentBackground) #设置对话框背景透明
        self.setWindowFlags(Qt.FramelessWindowHint)    #设置对话框无标题栏
        self.closehelpdia.clicked.connect(self.close)  #绑定关闭按钮关闭窗口
        self.donate_button.clicked.connect(self.showDonate) #绑定打赏按钮弹出打赏对话框

    def showDonate(self):               #定义弹出打赏对话框
        self.donateWin = donateDialog()
        self.donateWin.show()

    def mousePressEvent(self, event):                           #
        if event.button() == Qt.LeftButton:                     #
            self.m_flag = True                                  #
            self.m_position = event.globalPos() - self.pos()    #
            self.setCursor(Qt.OpenHandCursor)                   #此三函数
                                                                #用于移动
    def mouseMoveEvent(self, QMouseEvent):                      #窗口
        if Qt.LeftButton and self.m_flag:                       #
            self.move(QMouseEvent.globalPos() - self.m_position)#
                                                                #
    def mouseReleaseEvent(self, QMouseEvent):                   #
        self.m_flag = False                                     #
        self.setCursor(Qt.ArrowCursor)                          #

    def paintEvent(self, event):        #绘制对话框背景
        p = QPainter(self)
        p.setBrush(QBrush(QPixmap(':/image/image/background.jpg'))) #加入图片
        p.setRenderHint(QPainter.Antialiasing)
        p.drawRoundedRect(0, 0, self.width(), self.height(), 15, 15) #窗口倒圆角



class donateDialog(QMainWindow, Ui_donateDialog):       #定义打赏对话框类
    def __init__(self):
        super(donateDialog, self).__init__()
        self.setupUi(self)
        self.move(myWin.x(), myWin.y()+myWin.height())  #让打赏对话框靠主窗口下方停靠
        self.setWindowFlags(Qt.FramelessWindowHint)     #设置对话框无标题栏
        self.setAttribute(Qt.WA_TranslucentBackground)  #设置对话框背景透明
        self.closedonate.clicked.connect(self.close)    #绑定关闭按钮关闭窗口

    def mousePressEvent(self, event):                   #以下三函数定义移动窗口
        if event.button() == Qt.LeftButton:
            self.m_flag = True
            self.m_position = event.globalPos() - self.pos()
            self.setCursor(Qt.OpenHandCursor)

    def mouseMoveEvent(self, QMouseEvent):
        if Qt.LeftButton and self.m_flag:
            self.move(QMouseEvent.globalPos() - self.m_position)

    def mouseReleaseEvent(self, QMouseEvent):
        self.m_flag = False
        self.setCursor(Qt.ArrowCursor)

    def paintEvent(self, event):          #绘制窗口背景
        p = QPainter(self)
        p.setBrush(QBrush(QPixmap(':/image/image/background.jpg'))) #加入背景图片
        p.setRenderHint(QPainter.Antialiasing)
        p.drawRoundedRect(0, 0, self.width(), self.height(), 15, 15) #窗口倒圆角

if __name__ == '__main__':           #主函数
    app = QApplication(sys.argv)
    myWin = MyMainWindow()
    myWin.show()
    sys.exit(app.exec_())
